package com.launch.carmanager.common;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Looper;

import com.blankj.utilcode.util.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Properties;

/**
 * Created by ganquan on 2019/3/28 10:40
 */
public class AppConfigInfo {

    /**
     * 判断杀掉后是不是正常启动了应用, 在配置的mainactivity的时候设置true
     */
    public static boolean isOkStartApp;

    /**
     * 当前应用的上下文对象
     */
    public static Context context;

    /**
     * 资源对象
     */
    public static Resources resouce;

    /**
     * 应用包名
     */
    public static String packageName;

    /**
     * handler对象
     */
    public static Handler handler;

    /**
     * 应用的版本
     */
    public static String app_version = "";

    /**
     * 应用版本编号
     */
    public static int app_version_code;

    /**
     * 应用配置信息对象
     */
    public static AppConfigInfo appInfo;

    /**
     * 是否是调试开关
     */
    public boolean debug;
    /**
     * 是否成功加载授权类库
     */
    public static boolean isLICENSE = false;

    //钱包广告Id
    public final static String WALLET_ID = "100693";

    /**
     * 配置appinfo的信息
     *
     * @param context 当前的上下文
     */
    public static void setAppInfo(Context context) throws NullPointerException {
        if (context == null)
            throw new NullPointerException("context is not null and config info is not null");
        AppConfigInfo.context = context;
        AppConfigInfo.resouce = context.getResources();
        AppConfigInfo.packageName = context.getPackageName();
        AppConfigInfo.handler = new Handler(Looper.getMainLooper());
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
            AppConfigInfo.app_version = info.versionName;
            AppConfigInfo.app_version_code = info.versionCode;
            Properties prop = new Properties();
            InputStream inputStream = context.getAssets().open("app_six.properties");
            prop.load(inputStream);
            inputStream.close();
            // 配置app的一些信息id
            setValue(prop);
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param @param properties
     * @return void
     * @throws
     * @Title: setValue
     * @Description: TODO(设置每个配置的值)
     */
    private static void setValue(Properties properties) {
        if (appInfo == null) {
            appInfo = new AppConfigInfo();
        }
        Class userCla = appInfo.getClass();
        Field[] fs = userCla.getDeclaredFields();
        try {
            int fieldLength = fs.length;
            for (int i = 0; i < fieldLength; i++) {
                Field f = fs[i];
                f.setAccessible(true); //设置些属性是可以访问的
                Class class1 = f.getType();
                String name = f.getName();
                if (!StringUtils.isEmpty(name)) {
                    if (properties.containsKey(name)) {
                        String value = properties.getProperty(name);
                        if (StringUtils.isEmpty(value))
                            continue;
                        if (class1.equals(String.class)) {
                            f.set(appInfo, value); //给属性设值
                        } else if (class1.equals(Boolean.class) || class1.equals(boolean.class)) {
                            f.set(appInfo, value.equals("1") ? true : false); //给属性设值
                        } else if (class1.equals(Integer.class) || class1.equals(int.class)) {
                            f.set(appInfo, Integer.parseInt(value));
                        }
                    }
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    public static boolean isDebug() {
        return appInfo != null && appInfo.debug;
    }

    public static void setDebug(boolean isDebug) {
        if (appInfo != null) {
            appInfo.debug = isDebug;
        }
    }



    public static String getAppVersion() {
        if (isDebug()) {
            return app_version + "";
        } else {
            //todo
            return app_version + "";
        }
    }

    /**
     * 释放资源
     */
    public static void release() {
        appInfo = null;
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
        resouce = null;
        context = null;
    }
}
